package com.xhblogs.book.controller;

import com.xhblogs.book.pojo.User;
import com.xhblogs.book.service.LoginService;
import com.xhblogs.book.service.RegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@Service
@CrossOrigin
@RequestMapping("/api/v1/register")
public class RegisterController {

    @Autowired
    private LoginService loginService;

    @Autowired
    private RegisterService registerService;

    @ResponseBody
    @PostMapping("/register")
    public Object registerService(@RequestParam("username") String username, @RequestParam("password") String password, @RequestParam("email") String email) {
        User user = loginService.isUserLogin(username, password);
        HashMap<String, Object> map = new HashMap<>();
        if (user == null) {
           int status = registerService.register(username, password, email);
           map.put("isOk", status > 0);

        }else {
            map.put("isOk", false);
        }
        return map;
    }
}
